<?php

/*
 * 依门店代码抓hybris配货单
 */

$serverFilePath =realpath(dirname(__FILE__)); ;//realpath(dirname(__FILE__)); //$_SERVER["SCRIPT_FILENAME"];

$doc_dir = strrpos($serverFilePath, ''.DIRECTORY_SEPARATOR.'crontab');

$doc_dir = substr($serverFilePath, 0, $doc_dir + 1);   //project root

include_once $doc_dir . ''.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'lib/Config.php';
include_once $doc_dir . ''.DIRECTORY_SEPARATOR.'plugin'.DIRECTORY_SEPARATOR.'MaiscrmClass.php';
include_once $doc_dir . ''.DIRECTORY_SEPARATOR.'plugin'.DIRECTORY_SEPARATOR.'Logging.php';
include_once $doc_dir . ''.DIRECTORY_SEPARATOR.'plugin'.DIRECTORY_SEPARATOR.'GaoDeAPIClass.php';

set_time_limit(0);
error_reporting(E_ALL);
header("Content-type:text/html;charset:utf-8");

$config_ini = $doc_dir . 'config/config.ini';

$ini_array = parse_ini_file($config_ini, true);
$db_array = $ini_array['DB'];
$getWebsite = $ini_array['WebSite'];
$con = mysqli_init();

mysqli_real_connect($con, $db_array['host'], $db_array['id'], $db_array['pwd'], $db_array['database'], $db_array['port']);
mysqli_set_charset($con, $db_array['encoding']);
$sql = " SELECT buyer,ph_no,shop_no,check_num_maiscrm FROM orders WHERE maiscrm_mid = 'check_num_maiscrm' AND check_num_maiscrm < 3 AND check_num_maiscrm != 99 AND isdeleted = 0";

$result = mysqli_query($con, $sql);
$maiscrm_m = [];
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    $maiscrm_m[] = $row;
}

$log = new Logging();
$log->lfile('maiscrm_member');

$log->lwrite (json_encode($maiscrm_m, JSON_UNESCAPED_UNICODE));
if($maiscrm_m && count($maiscrm_m) > 0){
    foreach ($maiscrm_m as $k => $value){
        $Maiscrm = new MaiscrmClass($getWebsite['debug']);
        $MaiscrmMemberInfo = $Maiscrm->GetMemberInfo($value['buyer']);
        $log->lwrite (json_encode($MaiscrmMemberInfo, JSON_UNESCAPED_UNICODE));
        $field = [];
        if($MaiscrmMemberInfo && count($MaiscrmMemberInfo) > 0){
            $field['buyer']               =   $MaiscrmMemberInfo['name'];
            $field['buyer_mobile']        =   $MaiscrmMemberInfo['phone'];
            $field['maiscrm_mid']         =   $value['buyer'];
            $field['check_num_maiscrm']   =   99;
        }else{
            $field['check_num_maiscrm']   =   $value['check_num_maiscrm'] + 1;
        }
        $values = updateValue($field);

        $sql = "UPDATE `orders` set {$values}  WHERE  `ph_no` = '{$value['ph_no']}' AND `shop_no` = '{$value['shop_no']}'   ";

        $result = mysqli_query($con, $sql);
        if (!$result) {
            $log->lwrite (date('Y-m-d H:i:s')."门店:".$value['shop_no'].', 更新失败:' . $value['ph_no'] .','.json_encode($value,JSON_UNESCAPED_UNICODE). "\n");
        }
    }
}

function updateValue($arr) {
    $result = array();
    foreach ($arr as $k => $val) {
        $result[] = "`" . $k . "`" . "='" . $val . "'";
    }
    $result[] = "`updatedt`" . "=now() ";

    $re = implode(",", $result);
    return $re;
}
